package hrms.service;

import hrms.dao.FinanceMapper;
import hrms.dao.SalaryConfigInfoMapper;
import hrms.dao.SalaryMapper;
import hrms.entity.Finance;
import hrms.entity.Salary;
import hrms.entity.SalaryConfigInfo;
import hrms.util.TaxInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("SalaryService")
public class SalaryService {
    Calendar calendar = Calendar.getInstance();

    @Autowired
    SalaryConfigInfoMapper salaryConfigInfoMapper;

    @Autowired
    SalaryMapper salaryMapper;

    @Autowired
    FinanceMapper financeMapper;

    public List<Salary> findByDateOrStaffId(Integer Year, Integer month, Integer staffId){
        return salaryMapper.selectByDateOrStaffId(Year, month, staffId);
    }

    public void updateFinance(Salary salary){
        System.out.println(salary);
        // 更新月财务数据
        calendar.setTime(salary.getCreateTime());
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        double medicalPublic = salary.getMedicalPublic();
        double unemploymentPublic = salary.getUnemploymentPublic();
        double employmentInjuryPublic = salary.getEmploymentInjuryPublic();
        double endowmentPublic = salary.getEndowmentPublic();
        double maternityPublic = salary.getMaternityPublic();
        double housingProvidentFundPublic = salary.getHousingProvidentFundPublic();
        double base = salary.getBase();
        double add = salary.getAdd();
        double summary = medicalPublic + unemploymentPublic + employmentInjuryPublic + endowmentPublic
                + maternityPublic + housingProvidentFundPublic + base + add;
        System.out.println(summary);
        List<Finance> finances = financeMapper.selectByDate(year, month);
        Finance finance ;
        if(finances.size() == 0){
            finance = new Finance();
            finance.setMedicalPublic(medicalPublic);
            finance.setUnemploymentPublic(unemploymentPublic);
            finance.setEmploymentInjuryPublic(employmentInjuryPublic);
            finance.setEndowmentPublic(endowmentPublic);
            finance.setMaternityPublic(maternityPublic);
            finance.setHousingProvidentFundPublic(housingProvidentFundPublic);
            finance.setBase(base);
            finance.setAdd(add);
            finance.setSummary(summary);
            finance.setCreateTime(new Date());
            financeMapper.insert(finance);
        }else {
            finance = finances.get(0);
            finance.setMedicalPublic(medicalPublic + finance.getMedicalPublic());
            finance.setUnemploymentPublic(unemploymentPublic + finance.getUnemploymentPublic());
            finance.setEmploymentInjuryPublic(employmentInjuryPublic + finance.getEmploymentInjuryPublic());
            finance.setEndowmentPublic(endowmentPublic + finance.getEndowmentPublic());
            finance.setMaternityPublic(maternityPublic + finance.getMaternityPublic());
            finance.setHousingProvidentFundPublic(housingProvidentFundPublic + finance.getHousingProvidentFundPublic());
            finance.setBase(base + finance.getBase());
            finance.setAdd(add + finance.getAdd());
            finance.setSummary(summary + finance.getSummary());
            financeMapper.updateByPrimaryKey(finance);
        }
    }

    public Salary calSalary(Salary salary){
        Salary calSalary = new Salary();
        double tax;// 应缴个税=（（基本工资+奖金）-（五险一金个人部分）-起征点）*对应税率-速算
        double real;// 实际工资
        double sum = salary.getBase() + salary.getAdd();// 缴费基数
        SalaryConfigInfo salaryConfigInfo = salaryConfigInfoMapper.selectInfo();
        // 五险一金的计算
        double medicalPrivate = sum * salaryConfigInfo.getMedicalPrivate();
        double medicalPublic = sum * salaryConfigInfo.getMedicalPublic();
        double unemploymentPrivate = sum * salaryConfigInfo.getUnemploymentPrivate();
        double unemploymentPublic = sum * salaryConfigInfo.getUnemploymentPublic();
        double employmentInjuryPrivate = sum * salaryConfigInfo.getEmploymentInjuryPrivate();
        double employmentInjuryPublic = sum * salaryConfigInfo.getEmploymentInjuryPublic();
        double endowmentPrivate = sum * salaryConfigInfo.getEndowmentPrivate();
        double endowmentPublic = sum * salaryConfigInfo.getEndowmentPublic();
        double maternityPrivate = sum * salaryConfigInfo.getMaternityPrivate();
        double maternityPublic = sum * salaryConfigInfo.getMaternityPublic();
        double housingProvidentFundPrivate = sum * salaryConfigInfo.getHousingProvidentFundPrivate();
        double housingProvidentFundPublic = sum * salaryConfigInfo.getHousingProvidentFundPublic();
        // 个人应交五险一金总和
        double fiveAndOne = medicalPrivate + unemploymentPrivate + employmentInjuryPrivate
                + endowmentPrivate + maternityPrivate + housingProvidentFundPrivate;
        double baseTax = salaryConfigInfo.getBaseTax(); // 个税起征点
        double taxPayable = sum - fiveAndOne - baseTax;// 应纳税收入
        // 个人所得税计算
        if(taxPayable <= 3000){
            tax = taxPayable * TaxInfo.tax0_3k - TaxInfo.tax0_3kSub;
        }else if(taxPayable <= 12000){
            tax = taxPayable * TaxInfo.tax3_12k - TaxInfo.tax3_12kSub;
        }else if(taxPayable <= 25000){
            tax = taxPayable * TaxInfo.tax12_25k - TaxInfo.tax12_25kSub;
        }else if(taxPayable <= 35000){
            tax = taxPayable * TaxInfo.tax25_35k - TaxInfo.tax25_35kSub;
        }else if(taxPayable <= 55000){
            tax = taxPayable * TaxInfo.tax35_55k - TaxInfo.tax35_55kSub;
        }else if(taxPayable <= 80000){
            tax = taxPayable * TaxInfo.tax55_80k - TaxInfo.tax55_80kSub;
        }else{
            tax = taxPayable * TaxInfo.tax80k - TaxInfo.tax80kSub;
        }
        real = sum - fiveAndOne - tax;
        salary.setReal(real);
        salary.setMedicalPrivate(medicalPrivate);
        salary.setMedicalPublic(medicalPublic);
        salary.setUnemploymentPrivate(unemploymentPrivate);
        salary.setUnemploymentPublic(unemploymentPublic);
        salary.setEmploymentInjuryPrivate(employmentInjuryPrivate);
        salary.setEmploymentInjuryPublic(employmentInjuryPublic);
        salary.setEndowmentPrivate(endowmentPrivate);
        salary.setEndowmentPublic(endowmentPublic);
        salary.setMaternityPrivate(maternityPrivate);
        salary.setMaternityPublic(maternityPublic);
        salary.setHousingProvidentFundPrivate(housingProvidentFundPrivate);
        salary.setHousingProvidentFundPublic(housingProvidentFundPublic);
        salary.setTax(tax);
        salaryMapper.insert(salary);
        updateFinance(salary);
        return salary;
    }
}
